import baseservice from './baseservice.js';
export default {
	getPageData(self, successBack, errorBack) {
		baseservice.getPageData(self, self.apis.GetPageUsers, self.searchcondition);
	},
	getPageDataRole(self, successBack, errorBack) {
		let searchcondition = {};
		searchcondition = self.searchcondition;
		searchcondition = {};
		baseservice.getPageData(self, self.apis.roleList, searchcondition);
	},
	getModelData(self, id, successBack, errorBack) {
		let param = {
			id: self.id
		}
		self.httptool.ajaxPost(self.apis.GetUserDetail, param, function(res) {
			if(res.code == 1) {
				if(!res.data) {
					return;
				}
				self.modelData = self.formattool.trimNull(res.data, {});
				let selectedroles = res.data.roleList;
				let roles = [];
				if(selectedroles.length > 0) {
					for(var i = 0; i < selectedroles.length; i++) {
						if(selectedroles[i].flag == 1) {
							roles.push(selectedroles[i].id);
						}
					}
				}
				self.selectedroles = roles;
			}
		});
	},
	showDetail(self, row, title) {
		baseservice.showDetail(self, row, title);
	},
	GetUserServiceList(self,successBack, errorBack){
		self.httptool.ajaxPost(self.apis.GetUserServiceList,{},function(res){
			debugger;
			successBack(res);
		});
	},
	handleSave(self, successBack, errorBack) {
		let that = this;
		//提交验证
		let rule = [{
			name: 'userName',
			label: '用户名称',
			rule: ['validateEmpty_']
		}, {
			name: 'userCount',
			label: '用户账号',
			rule: ['validateEmpty_']
		}, {
			name: 'phone',
			label: '电话',
			rule: ['validateEmpty_', 'validateTel_']
		}]

		if(!self.validatetool.validate(self, self.modelData, rule)) {
			return;
		}

		let requestUrl = self.apis.addUser;
		let msg = '添加成功';
		if(self.modelData.id > 0) {
			msg = '修改成功';
			requestUrl = self.apis.updateUser;
		} else {
			self.modelData.password = '';
			self.modelData.userIconUrl = '';

		}

		self.modelData.roleIds = self.selectedroles;
		self.httptool.ajaxPost(requestUrl, self.modelData, function(res) {
			if(res.code == 1) {
				self.$message({
					message: msg,
					type: 'success'
				});
				self.close();
				self.$emit('close', true);
			} else {
				self.$message(res.msg);
			}
		});

	},
	getRole(self, successBack, errorBack) {
		let param = {};
		self.httptool.ajaxPost(self.apis.roleList, param, function(res) {
			let roleList = [];
			roleList = res.data;
			self.roleList = roleList;
		});

	},
	login(self, model, successBack, errorBack) {
		//提交验证
		let rule = [{
			name: 'userCount',
			label: '账号',
			rule: ['validateEmpty_']
		}, {
			name: 'password',
			label: '密码',
			rule: ['validateEmpty_']
		}]

		if(!self.validatetool.validate(self, model, rule)) {
			return;
		}
		self.httptool.ajaxPost(self.apis.login, model, (res) => {
			let data = res.data;
			localStorage.setItem('ms_username', self.modelData.userCount);
			sessionStorage.setItem('userName', data.user.userName);
			sessionStorage.setItem('token', data.token);
			sessionStorage.setItem('userId', data.user.id);
			sessionStorage.setItem('userType', data.user.userType); //区分贸易商或者平台管理员
			sessionStorage.setItem('userCount', data.user.userCount);
			
			sessionStorage.setItem('hxAccount',JSON.stringify(data.hxAccount))			
			sessionStorage.setItem('my_user_info',JSON.stringify(data.user))
			
			self.$store.state.userId = data.user.id;

			self.$store.state.userInfo = data.user;
			self.$message({
				message: '登录成功！',
				type: 'success'
			});
			self.$router.push('/dashboard');

		}, function(res) {
			self.$message({
				message: res.msg,
				type: 'error'
			});
		});
	}
}